home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Programming / MiniGL / demos / texsub.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-07  |  6.1 KB  |  215 lines

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*  texsub.c
  39.  *  This program texture maps a checkerboard image onto
  40.  *  two rectangles.  This program clamps the texture, if
  41.  *  the texture coordinates fall outside 0.0 and 1.0.
  42.  *  If the s key is pressed, a texture subimage is used to
  43.  *  alter the original texture.  If the r key is pressed, 
  44.  *  the original texture is restored.
  45.  */
  46.  
  47. /*
  48.  * MiniGL adaption by Hans-Joerg Frieden
  49.  * December 1999
  50.  */
  51.  
  52. #include <mgl/gl.h>
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <math.h>
  56.  
  57. /*  Create checkerboard textures  */
  58. #define checkImageWidth 64
  59. #define checkImageHeight 64
  60. #define subImageWidth 16
  61. #define subImageHeight 16
  62. static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
  63. static GLubyte subImage[subImageHeight][subImageWidth][4];
  64.  
  65. static GLuint texName;
  66.  
  67. void makeCheckImages(void)
  68. {
  69.    int i, j, c;
  70.     
  71.    for (i = 0; i < checkImageHeight; i++) {
  72.       for (j = 0; j < checkImageWidth; j++) {
  73.      c = ((((i&0x8)==0)^((j&0x8)==0)))*255;
  74.      checkImage[i][j][0] = (GLubyte) c;
  75.      checkImage[i][j][1] = (GLubyte) c;
  76.      checkImage[i][j][2] = (GLubyte) c;
  77.      checkImage[i][j][3] = (GLubyte) 255;
  78.       }
  79.    }
  80.    for (i = 0; i < subImageHeight; i++) {
  81.       for (j = 0; j < subImageWidth; j++) {
  82.      c = ((((i&0x4)==0)^((j&0x4)==0)))*255;
  83.      subImage[i][j][0] = (GLubyte) c;
  84.      subImage[i][j][1] = (GLubyte) 0;
  85.      subImage[i][j][2] = (GLubyte) 0;
  86.      subImage[i][j][3] = (GLubyte) 255;
  87.       }
  88.    }
  89. }
  90.  
  91. void init(void)
  92. {    
  93.    glClearColor (0.0, 0.0, 0.0, 0.0);
  94.    glShadeModel(GL_FLAT);
  95.    glEnable(GL_DEPTH_TEST);
  96.  
  97.    makeCheckImages();
  98.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  99.  
  100.    glGenTextures(1, &texName);
  101.    glBindTexture(GL_TEXTURE_2D, texName);
  102.  
  103.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  104.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  105.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  106.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  107.    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 
  108.         0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
  109. }
  110.  
  111. void display(void)
  112. {
  113.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  114.    glEnable(GL_TEXTURE_2D);
  115.    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  116.    glBindTexture(GL_TEXTURE_2D, texName);
  117.    glBegin(GL_QUADS);
  118.    glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);
  119.    glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);
  120.    glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0);
  121.    glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0);
  122.  
  123.    glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0);
  124.    glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0);
  125.    glTexCoord2f(1.0, 1.0); glVertex3f(2.41421, 1.0, -1.41421);
  126.    glTexCoord2f(1.0, 0.0); glVertex3f(2.41421, -1.0, -1.41421);
  127.    glEnd();
  128.    glFlush();
  129.    glDisable(GL_TEXTURE_2D);
  130.    mglUnlockDisplay();
  131.    mglSwitchDisplay();
  132. }
  133.  
  134. void reshape(int w, int h)
  135. {
  136.    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  137.    glMatrixMode(GL_PROJECTION);
  138.    glLoadIdentity();
  139.    gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 30.0);
  140.    glMatrixMode(GL_MODELVIEW);
  141.    glLoadIdentity();
  142.    glTranslatef(0.0, 0.0, -3.6);
  143. }
  144.  
  145. /* ARGSUSED1 */
  146. static void keyboard (char key)
  147. {
  148.    switch (key) {
  149.       case 's':
  150.       case 'S':
  151.      glBindTexture(GL_TEXTURE_2D, texName);
  152.      glTexSubImage2D(GL_TEXTURE_2D, 0, 12, 44, subImageWidth,
  153.              subImageHeight, GL_RGBA,
  154.              GL_UNSIGNED_BYTE, subImage);
  155.      break;
  156.       case 'r':
  157.       case 'R':
  158.      glBindTexture(GL_TEXTURE_2D, texName);
  159.      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
  160.               checkImageHeight, 0, GL_RGBA,
  161.               GL_UNSIGNED_BYTE, checkImage);
  162.      break;
  163.       case 27:
  164.      mglExit();
  165.      break;
  166.       default:
  167.      break;
  168.    }
  169. }
  170.  
  171. static void
  172. idle(void)
  173. {
  174.     display();
  175. }
  176.  
  177.  
  178. int main(int argc, char *argv[])
  179. {
  180.     GLint width=320; GLint height=240;
  181.     int i;
  182.  
  183.     for (i=1; i<argc; i++)
  184.     {
  185.     if (0 == stricmp(argv[i], "-width"))
  186.     {
  187.         i++;
  188.         width = atoi(argv[i]);
  189.     }
  190.     if (0 == stricmp(argv[i], "-height"))
  191.     {
  192.         i++;
  193.         height = atoi(argv[i]);
  194.     }
  195.     }
  196.  
  197.     mglChooseVertexBufferSize(1000);
  198.     MGLInit();
  199.     mglCreateContext(0,0,width,height);
  200.     mglEnableSync(GL_TRUE);
  201.  
  202.     init();
  203.  
  204.     glClearColor(0.0, 0.0, 0.0, 1.0);
  205.  
  206.     reshape(width,height);
  207.     mglLockMode(MGL_LOCK_SMART);
  208.     mglIdleFunc(idle);
  209.     mglKeyFunc(keyboard);
  210.     mglMainLoop();
  211.     mglDeleteContext();
  212.     MGLTerm();
  213.     return 0;             /* ANSI C requires main to return int. */
  214. }
  215.